// 文件路径: com.city.java.reports.service.impl.ReportProcessingServiceImpl.java
package com.city.java.reports.service.impl;

import com.city.java.reports.mapper.ReportProcessingMapper;
import com.city.java.reports.pojo.entity.ReportProcessing;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.reports.service.ReportProcessingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 举报处理服务实现类
 */
@Slf4j
@Service
public class ReportProcessingServiceImpl implements ReportProcessingService {

    @Autowired
    private ReportProcessingMapper reportProcessingMapper;

    @Override
    public JsonResult addReportProcessing(ReportProcessing reportProcessing) {
        log.debug("准备添加举报处理记录: {}", reportProcessing);

        // 参数校验
        if (reportProcessing == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报处理信息不能为空");
        }

        if (reportProcessing.getReportId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        // 设置创建时间
        if (reportProcessing.getCreatedAt() == null) {
            reportProcessing.setCreatedAt(LocalDateTime.now());
        }

        // 设置默认状态
        if (reportProcessing.getStatus() == null) {
            reportProcessing.setStatus(ReportProcessing.ProcessingStatus.pending);
        }

        try {
            reportProcessingMapper.insertReportProcessing(reportProcessing);
            log.info("举报处理记录添加成功: 处理ID={}", reportProcessing.getProcessingId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加举报处理记录失败: 举报ID={}", reportProcessing.getReportId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加举报处理记录失败");
        }
    }

    @Override
    public JsonResult getReportProcessingById(Long processingId) {
        log.debug("根据ID查询举报处理记录: {}", processingId);

        // 参数校验
        if (processingId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            ReportProcessing reportProcessing = reportProcessingMapper.selectById(processingId);
            if (reportProcessing != null) {
                log.info("查询举报处理记录成功: ID={}", processingId);
                return JsonResult.ok(reportProcessing);
            } else {
                log.warn("举报处理记录不存在: ID={}", processingId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报处理记录不存在");
            }
        } catch (Exception e) {
            log.error("查询举报处理记录失败: ID={}", processingId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报处理记录失败");
        }
    }

    @Override
    public JsonResult getReportProcessingByReportId(Long reportId) {
        log.debug("根据举报ID查询举报处理记录: {}", reportId);

        // 参数校验
        if (reportId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        try {
            ReportProcessing reportProcessing = reportProcessingMapper.selectByReportId(reportId);
            if (reportProcessing != null) {
                log.info("查询举报处理记录成功: 举报ID={}", reportId);
                return JsonResult.ok(reportProcessing);
            } else {
                log.warn("举报处理记录不存在: 举报ID={}", reportId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报处理记录不存在");
            }
        } catch (Exception e) {
            log.error("查询举报处理记录失败: 举报ID={}", reportId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报处理记录失败");
        }
    }

    @Override
    public JsonResult getReportProcessingsByStatus(ReportProcessing.ProcessingStatus status) {
        log.debug("根据状态查询举报处理列表: {}", status);

        // 参数校验
        if (status == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "状态不能为空");
        }

        try {
            List<ReportProcessing> reportProcessings = reportProcessingMapper.selectByStatus(status);
            log.info("查询状态举报处理列表成功: 状态={}, 记录数量={}", status, reportProcessings.size());
            return JsonResult.ok(reportProcessings);
        } catch (Exception e) {
            log.error("查询状态举报处理列表失败: 状态={}", status, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报处理列表失败");
        }
    }

    @Override
    public JsonResult getAllReportProcessings() {
        log.debug("查询所有举报处理列表");

        try {
            List<ReportProcessing> reportProcessings = reportProcessingMapper.selectAll();
            log.info("查询所有举报处理列表成功: 记录数量={}", reportProcessings.size());
            return JsonResult.ok(reportProcessings);
        } catch (Exception e) {
            log.error("查询所有举报处理列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报处理列表失败");
        }
    }

    @Override
    public JsonResult deleteReportProcessingById(Long processingId) {
        log.debug("根据ID删除举报处理记录: {}", processingId);

        // 参数校验
        if (processingId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            int result = reportProcessingMapper.deleteById(processingId);
            if (result > 0) {
                log.info("删除举报处理记录成功: ID={}", processingId);
                return JsonResult.ok();
            } else {
                log.warn("删除举报处理记录失败，记录不存在: ID={}", processingId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报处理记录不存在");
            }
        } catch (Exception e) {
            log.error("删除举报处理记录失败: ID={}", processingId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除举报处理记录失败");
        }
    }

    @Override
    public JsonResult updateReportProcessing(ReportProcessing reportProcessing) {
        log.debug("更新举报处理信息: {}", reportProcessing);

        // 参数校验
        if (reportProcessing == null || reportProcessing.getProcessingId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            // 检查举报处理记录是否存在
            ReportProcessing existingProcessing = reportProcessingMapper.selectById(reportProcessing.getProcessingId());
            if (existingProcessing == null) {
                log.warn("更新举报处理记录失败，记录不存在: ID={}", reportProcessing.getProcessingId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报处理记录不存在");
            }

            // 如果状态更新为已处理，设置处理时间
            if (reportProcessing.getStatus() != null &&
                reportProcessing.getStatus() == ReportProcessing.ProcessingStatus.processed &&
                existingProcessing.getStatus() != ReportProcessing.ProcessingStatus.processed) {
                reportProcessing.setProcessedAt(LocalDateTime.now());
            }

            int result = reportProcessingMapper.updateById(reportProcessing);
            if (result > 0) {
                log.info("更新举报处理记录成功: ID={}", reportProcessing.getProcessingId());
                return JsonResult.ok();
            } else {
                log.warn("更新举报处理记录失败: ID={}", reportProcessing.getProcessingId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新举报处理记录失败");
            }
        } catch (Exception e) {
            log.error("更新举报处理记录失败: ID={}", reportProcessing.getProcessingId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新举报处理记录失败");
        }
    }
}
